﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Service;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using $prjsn$.Common;
using $prjsn$.Common.Business;
using $prjsn$.Entity;

namespace $safeprojectname$.Controllers
{
    /// <summary>
    ///     Overrides base controller action 'ComboBox', to return AllVisible values
    /// </summary>
    public abstract class CodeListController<T, TFilter, TModel, TFacade>
        : EntityController<T, TFilter, TModel, TFacade>
        where T : class, ICodeList, new()
        where TFilter : CodeListFilter
        where TModel : class, IEditModel<T, int, TFilter>
        where TFacade : class, ICodeListFacade<T, TFilter>
    {
        #region OnList

        /// <summary>
        ///     Set columns for List View
        /// </summary>
        protected override IEnumerable<IItemToDisplay> LoadItemsToDisplay(TFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.ToDisplay(),
                    Items = new List<ICellDescription>
                    {
                        i.Cell(e => e.Code),
                        i.Cell(e => e.Description),
                        i.Cell(e => e.IsVisible, new CellDescription {Align = Align.center, IsBoolean = true,}),
                        i.Cell(e => e.ID, new CellDescription {Align = Align.right, Format = "N0",}),
                    }
                } as IItemToDisplay);
        }

        /// <summary>
        ///     Provides columns for Excel
        /// </summary>
        protected override IEnumerable<IEnumerable<IExCell>> LoadExcelItemsToDisplay(TFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new List<IExCell>
                {
                    i.ExCell(e => e.ID),
                    i.ExCell(e => e.Code),
                    i.ExCell(e => e.Description),
                    i.ExCell(e => e.IsVisible),
                });
        }

        #endregion OnList

        #region ComboBox

        /// <summary>
        ///     Returns the list of all Visible values
        ///     The list is rendered as ComboBox (JQuery event is triggered)
        /// </summary>
        /// <returns>rendered ComboBox</returns>
        public override ActionResult ComboBox()
        {
            var model = Factory.CreateInstance<IComboBoxModel>();

            BindModel(model);

            model.ComboBoxName = model.ComboBoxName.IsNotEmpty()
                                     ? model.ComboBoxName
                                     : ControllerName;

            var filter = ClearFilter();

            Facade.BindFilter(filter, ValueProvider.ToContainer());

            model.Items = new SelectList(Facade.GetAllVisible()
                , Str.Common.ID
                , Str.Common.Display
                , model.SelectedID);

            return View(Str.WebControls.ComboBox, model);
        }

        #endregion ComboBox

        #region AutoComplete

        protected override void OnAutoCompleteList(TFilter search, string term)
        {
            term = term ?? string.Empty;

            var list = Facade.GetAllVisible().Where(c => c.Description.StartsWith(term));

            Model.ListModel.ItemsToDisplay = list
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.Description,
                } as IItemToDisplay);
        }

        #endregion AutoComplete

        #region properties

        //ICodeListModel<T, TFilter> ICodeListController<T, TFilter>.Model
        //{
        //    get { return base.Model; }
        //}

        #endregion properties
    }
}